package dominia.widgets;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;

import dominia.SWTMain;

public abstract class SingletonDialog extends Dialog {
	protected static HashMap<String, SingletonDialog>	instances;
	protected static Logger					logger;

	protected Object					result;

	protected SingletonDialog()
	{
		super(SWTMain.getTopShell(), SWT.APPLICATION_MODAL);
		if (logger == null)
			logger = Logger.getLogger(this.getClass());
	}

	protected SingletonDialog(String title)
	{
		this();
		this.setText(title);
	}

	protected static SingletonDialog getInstance(Class<? extends SingletonDialog> cl)
	{
		SingletonDialog inst = null;
		String name = cl.getName();

		if (logger == null)
			logger = Logger.getLogger(cl);

		if (instances == null) {
			instances = new HashMap<String,SingletonDialog>();
		} else {
			inst = instances.get(name);
		}

		if (inst == null) {
			try {
				inst = (SingletonDialog)cl.newInstance();
				instances.put(name, inst);
			} catch (InstantiationException e) {
				logger.error("", e);
			} catch (IllegalAccessException e) {
				logger.error("", e);
			}
		}

		return inst;
	}
}